﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBHelper;
using SME.Food.Surperise.Model;

namespace SME.Food.Surperise.DAL
{
	/// <summary>
	/// 数据访问类:Bas_User
	/// </summary>
	public partial class Bas_User_DAL
	{
        public Bas_User_DAL()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "Bas_User"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Bas_User");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString());
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Bas_User_Model model)
		{
            string strSql = string.Format(@"insert into Bas_User values ('{0}','{1}','{2}','{3}','{4}')", model.User_No, model.User_Name, model.User_PassWord, model.Mem_No, model.Mem_Power);
			int i = DbHelperSQL.ExcuteNoQuery(strSql);
		    return i;
			
		}
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passWord"></param>
        /// <param name="userNo"></param>
        /// <returns></returns>
        public bool Update(string passWord, string userNo)
	    {
	        string sql = string.Format("update Bas_User set User_PassWord='" + passWord + "' where User_No='"+userNo+"'");
            int rows = DbHelperSQL.ExcuteNoQuery(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
	    }


	    /// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(Bas_User_Model model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Bas_User set ");
            strSql.Append(" User_No='" + model.User_No+"'");
            strSql.Append(", User_Name='" + model.User_Name + "'");
            strSql.Append(", User_PassWord='" + model.User_PassWord + "'");
            strSql.Append(", Mem_No='" + model.Mem_No + "'");
            strSql.Append(", Mem_Power=" + model.Mem_Power );
            strSql.Append(" where User_No='" + model.User_No + "'");
            int rows = DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bas_User ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bas_User ");
			strSql.Append(" where User_No in ("+IDlist + ")  ");
            int rows = DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Bas_User_Model GetModel(string userNo)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,User_No,User_Name,User_PassWord,Mem_No,Mem_Power from Bas_User ");
            strSql.Append(" where User_No=" + userNo);

            Bas_User_Model model = new Bas_User_Model();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bas_User_Model DataRowToModel(DataRow row)
        {
            Bas_User_Model model = new Bas_User_Model();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["User_No"] != null)
                {
                    model.User_No = row["User_No"].ToString();
                }
                if (row["User_Name"] != null)
                {
                    model.User_Name = row["User_Name"].ToString();
                }
                if (row["User_PassWord"] != null)
                {
                    model.User_PassWord = row["User_PassWord"].ToString();
                }
                if (row["Mem_No"] != null)
                {
                    model.Mem_No = row["Mem_No"].ToString();
                }
                if (row["Mem_Power"] != null && row["Mem_Power"].ToString() != "")
                {
                    model.Mem_Power = int.Parse(row["Mem_Power"].ToString());
                }
            }
            return model;
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,User_No,User_Name,User_PassWord,Mem_No,Mem_Power ");
			strSql.Append(" FROM Bas_User ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

